﻿using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UpdateServers.Command
{
    /// <summary>
    /// 文件压缩，解压类
    /// </summary>
    public class ZipHelper
    {

        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="ZipRoot">压缩文件地址</param>
        /// <param name="ToDir">解压目录</param>
        /// <returns></returns>
        public static bool UnZip(string ZipRoot, string ToDir)
        {
            var result = false;

            if (File.Exists(ZipRoot))
            {
                try
                {
                    if (!Directory.Exists(ToDir))
                    {
                        Directory.CreateDirectory(ToDir);
                    }
                    ZipFile.ExtractToDirectory(ZipRoot, ToDir,Encoding.GetEncoding("GBK"), true);
                    result = true;
                }
                catch (Exception ex)
                {
                    Loger.Log(ex.Message, "Error");
                }
            }

            return result;
        }
        /// <summary>
        /// 创建压缩文件
        /// </summary>
        /// <param name="DirRoot">源文件夹</param>
        /// <param name="ZipRoot">目标压缩文件路径</param>
        /// <returns></returns>
        public static bool CreatZip(string DirRoot,string ZipRoot) {
            var result = false;
            if (Directory.Exists(DirRoot))
            {
                try
                {
                    ZipFile.CreateFromDirectory(DirRoot, ZipRoot);
                    result = true;
                }
                catch (Exception ex)
                {
                    Loger.Log(ex.Message, "Error");
                }
             
            }
            return result;
        }

    }
}
